package com.excesys.exsecs.alarm.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.alarm.entity.AlarmTypeConf;
import com.excesys.exsecs.alarm.service.AlarmTypeConfService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 告警类型配置表
 *
 * @author lxc
 * @date 2024-10-14 16:40:36
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmTypeConf" )
@Tag(description = "alarmTypeConf" , name = "告警类型配置表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmTypeConfController {

    private final AlarmTypeConfService alarmTypeConfService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param alarmTypeConf 告警类型配置表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_view')" )
    public R getPage(@ParameterObject Page page, @ParameterObject AlarmTypeConf alarmTypeConf) {
        return R.ok(alarmTypeConfService.queryPage(page, alarmTypeConf));
    }


    /**
     * 通过id查询告警类型配置表
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(alarmTypeConfService.getTypeById(id));
    }

    /**
     * 新增告警类型配置表
     * @param alarmTypeConf 告警类型配置表
     * @return R
     */
    @Operation(summary = "新增告警类型配置表" , description = "新增告警类型配置表" )
    @SysLog("新增告警类型配置表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_add')" )
    public R save(@RequestBody AlarmTypeConf alarmTypeConf) {
        return R.ok(alarmTypeConfService.saveType(alarmTypeConf));
    }

    /**
     * 修改告警类型配置表
     * @param alarmTypeConf 告警类型配置表
     * @return R
     */
    @Operation(summary = "修改告警类型配置表" , description = "修改告警类型配置表" )
    @SysLog("修改告警类型配置表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_edit')" )
    public R updateType(@RequestBody AlarmTypeConf alarmTypeConf) {
        if (ObjectUtil.isEmpty(alarmTypeConf.getId())){
            return R.failed("未找到该配置项");
        }
        return R.ok(alarmTypeConfService.updateType(alarmTypeConf));
    }

    /**
     * 通过id删除告警类型配置表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除告警类型配置表" , description = "通过id删除告警类型配置表" )
    @SysLog("通过id删除告警类型配置表" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_del')" )
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(alarmTypeConfService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param alarmTypeConf 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('alarm_alarmTypeConf_export')" )
    public List<AlarmTypeConf> export(AlarmTypeConf alarmTypeConf,Integer[] ids) {
        return alarmTypeConfService.list(Wrappers.lambdaQuery(alarmTypeConf).in(ArrayUtil.isNotEmpty(ids), AlarmTypeConf::getId, ids));
    }
}